package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.Device;
import com.cjb.cloudsystem.mapper.DeviceMapper;
import com.cjb.cloudsystem.service.DeviceService;
import com.cjb.cloudsystem.vo.DeviceVO;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import com.cjb.common.util.Constant;
import com.cjb.common.util.JsonUtils;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Author chenjinbin
* @Package IntelliJ IDEA
* @Class DevicesServiceImpl
* @Date 2023-07-27 21:41
*/
@Service
@AllArgsConstructor
public class DeviceServiceImpl extends BaseRepositoryImpl<DeviceMapper, Device> implements DeviceService {
/**
* 日志
*/
    private final static Logger log = LoggerFactory.getLogger(DeviceServiceImpl.class);
    /*
    * 注入mapper层
    */
    private final DeviceMapper deviceMapper;
    /**
    * 单体保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public boolean save(Device device) {
        if(Common.isEmpty(device)){
            throw new ServerException("sysObject is empty");
        }
        log.info("查看输入值{}", JsonUtils.toJsonString(device));
        device.setDeleted(Constant.NO);
        baseMapper.insert(device);
        return true;
    }

    /**
    *批量保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Integer listSave(List<Device> device) {
        return baseMapper.insertBatchSomeColumn(device);
    }

    /**
    * 批量更新
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listUpdate(List<Device> device) {
        saveOrUpdateBatch(device,device.size());
    }

    /**
    * 批量删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listDelete(List<Device> device) {
        List<Long> collect = device.stream().map(Device::getDeviceId)
        .distinct()
        .collect(Collectors.toList());
        deviceMapper.deleteBatchIds(collect);
    }
    /**
    * 通过id删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public int deleteById(Device device) {
        return deviceMapper.deleteById(device.getDeviceId());
    }

    /**
    *单个查询
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Device queryById(Device device) {
        log.info("打印设备参数{}",JsonUtils.toJsonString(device));
        if (Common.isNotEmpty(device.getDeviceId())){
            log.info("打印设备信息 {}",JsonUtils.toJsonString(device));
            return  deviceMapper.selectById(device.getDeviceId());
        }
        else {
            log.info("结果为空");
            return null;
        }

    }
    /**
    *分页
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Page queryPage(Page devicePage , DeviceVO deviceVO) {
        return deviceMapper.selectPage(devicePage, getWrapper(deviceVO));
    }
    /**
    *查询属性
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    private LambdaQueryWrapper<Device> getWrapper(DeviceVO query) {
    LambdaQueryWrapper<Device> wrapper = Wrappers.lambdaQuery();
                if(Common.isNotNull(query.getDeviceId())){wrapper.eq(Device::getDeviceId,query.getDeviceId());}
                if(Common.isNotNull(query.getDeviceName())){wrapper.like(Device::getDeviceName,"%"+query.getDeviceName()+"%");}
                if(Common.isNotNull(query.getDeviceOwner())){wrapper.like(Device::getDeviceOwner,"%"+query.getDeviceOwner()+"%");}
                if(Common.isNotNull(query.getDeviceNumber())){wrapper.like(Device::getDeviceNumber,"%"+query.getDeviceNumber()+"%");}
                if(Common.isNotNull(query.getDeleted())){wrapper.eq(Device::getDeleted,query.getDeleted());}
                if(Common.isNotNull(query.getCreateTime())){wrapper.eq(Device::getCreateTime,query.getCreateTime());}
                if(Common.isNotNull(query.getUpdateTime())){wrapper.eq(Device::getUpdateTime,query.getUpdateTime());}
                if(Common.isNotNull(query.getCreateor())){wrapper.eq(Device::getCreateor,query.getCreateor());}
                if(Common.isNotNull(query.getUpdater())){wrapper.eq(Device::getUpdater,query.getUpdater());}

                wrapper.eq(Device::getDeleted,0);
        wrapper.orderByDesc(Device::getDeviceId);
        return wrapper;
    }



    /**
    * 返回全部信息
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public List<Device> returnAllData() {
        return deviceMapper.selectList(null);
    }

}